package com.cn.steam.iam.model;

import com.cn.steam.iam.model.po.IamPartner;
import com.google.common.collect.Sets;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static com.cn.steam.foundation.common.model.CommonConstant.*;

/**
 * client detail包装类
 * 用于迭代完善iam
 *
 * @author huangzz
 */
public class ClientDetailsWrap implements ClientDetails {

    private IamUserDetails user;
    private IamPartner partner;

    public ClientDetailsWrap() {

    }

    public ClientDetailsWrap(IamUserDetails user, IamPartner iamPartner) {
        this.user = user;
        this.partner = iamPartner;
    }


    public IamUserDetails getUser() {
        return user;
    }

    public void setUser(IamUserDetails user) {
        this.user = user;
    }


    @Override
    public String getClientId() {
        return user.getLoginName();
    }

    @Override
    public Set<String> getResourceIds() {
        return null;
    }

    @Override
    public boolean isSecretRequired() {
        return true;
    }

    @Override
    public String getClientSecret() {
        return user.getPassword();
    }

    @Override
    public boolean isScoped() {
        return true;
    }

    @Override
    public Set<String> getScope() {
        return Sets.newHashSet("all");
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return user.getGrantTypeSet();
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return Sets.newHashSet("https://www.baidu.com");
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return user.getPermissionsSet();
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return 30 * 60;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return 35 * 60;
    }

    @Override
    public boolean isAutoApprove(String s) {
        return false;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        Map<String, Object> additional = new HashMap<>();
        additional.put(PARTNER_ID, user.getPartnerId());
        additional.put(USER_ID, user.getUserId());
        additional.put(EXT_PARTNER_ID, user.getExtPartnerId());
        additional.put(EXT_USER_ID, user.getExtUserId());
        additional.put(USER_TYPE, user.getType());
        additional.put(PARTNER_TYPE, partner.getType());
        return additional;
    }
}
